import RobotError from "../../errors/robot-error.js";
import Item from "../../item/item.js";
import Member from "./member.js";





export default class ItemM extends Member {
	items: Map<Item, number> = new Map();


	addItem(item: Item, num: number = 1): void {
		let lastNum = this.itemCount(item);
		this.setItemCount(item, lastNum + num);
	}
	removeItem(item: Item, num: number = 1): void {
		let lastNum = this.itemCount(item);
		if (lastNum < num) throw new RobotError(item.name + "不足");
		this.setItemCount(item, lastNum - num);
	}
	hasItem(item: Item): boolean {
		return this.itemCount(item) > 0;
	}
	itemCount(item: Item): number {
		let count = 0;
		for (let itm of this.items) {
			if (item.equals(itm[0])) {
				count = itm[1];
				break;
			}
		}
		if (count < 0) count = 0;
		return count;
	}
	setItemCount(item: Item, count: number) {
		for (let itm of this.items) {
			if (item.equals(itm[0])) {
				this.items.set(itm[0], count);
				return;
			}
		}
		this.items.set(item, count);
	}
}